// import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
// import 'package:rate_my_app/rate_my_app.dart';
import 'package:sinosheets/route/route_utils.dart';

import 'logic.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> with AutomaticKeepAliveClientMixin {
  final SettingsLogic logic = Get.put(SettingsLogic());

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<SettingsLogic>(builder: (get) {
      return Scaffold(
        backgroundColor: Color(0xFFF7F8FA),
        appBar: AppBar(
          title: Text('Settings', style: TextStyle(fontSize: 22.r, fontWeight: FontWeight.bold, color: Colors.black)),
          elevation: 0,
          centerTitle: true,
          iconTheme: IconThemeData(color: Colors.black),
        ),
        body: GridView.count(
          padding: EdgeInsets.symmetric(horizontal: 12.r, vertical: 16.r),
          crossAxisCount: 2,
          mainAxisSpacing: 16.r,
          crossAxisSpacing: 16.r,
          childAspectRatio: 0.85,
          children: [
            ///用户协议
            _settingGridItem(
              icon: Icons.description_outlined,
              iconBg: Color.fromARGB(255, 232, 241, 245),
              iconColor: Color.fromARGB(255, 76, 135, 175),
              title: 'User Agreement',
              subtitle: 'View our user agreement',
              onTap: () {
                RouteUtils.toWebView(url: 'https://sinosheets.com/agreement');
              },
            ),

            ///隐私政策
            _settingGridItem(
              icon: Icons.description_outlined,
              iconBg: Color.fromARGB(255, 242, 245, 232),
              iconColor: Color.fromARGB(255, 153, 175, 76),
              title: 'Privacy Policy',
              subtitle: 'View our privacy policy',
              onTap: () {
                RouteUtils.toWebView(url: 'https://sinosheets.com/policy');
              },
            ),

            ///收藏页面
            _settingGridItem(
              icon: Icons.favorite_border,
              iconBg: Color(0xFFE8F5E8),
              iconColor: Color(0xFF4CAF50),
              title: 'Favorite',
              subtitle: 'View your favorite items',
              onTap: () {
                RouteUtils.toFavorite();
              },
            ),

            // _settingGridItem(
            //   icon: Icons.info_outline,
            //   iconBg: Color(0xFFE1F5FE),
            //   iconColor: Color(0xFF00BCD4),
            //   title: 'About Us',
            //   subtitle: 'Learn more about our app',
            //   onTap: () {
            //     RouteUtils.toAboutUs();
            //   },
            // ),
            if (get.isShowCode)
              _settingGridItem(
                icon: Icons.add_box_outlined,
                iconBg: Color(0xFFE8F5E8),
                iconColor: Color(0xFF4CAF50),
                title: 'Set Promo Code',
                subtitle: 'Set the promo code you obtained',
                onTap: () {
                  RouteUtils.toAddCode();
                },
              ),
            // _settingGridItem(
            //   icon: Icons.star_border,
            //   iconBg: Color(0xFFF0E6FF),
            //   iconColor: Color(0xFF9C27B0),
            //   title: 'Rate Us',
            //   subtitle: 'Share your feedback',
            //   onTap: () {
            //     _rateUs();
            //   },
            // ),
            _settingGridItem(
              icon: Icons.cleaning_services_outlined,
              iconBg: Color(0xFFFFF3E0),
              iconColor: Color(0xFFFF5722),
              title: 'Clear Cache',
              subtitle: 'Current size: ${get.cacheText}',
              onTap: () {
                get.onClearCacheClick();
                Get.back();
              },
            ),
          ],
        ),
      );
    });
  }

  Widget _settingGridItem({
    required IconData icon,
    required Color iconBg,
    required Color iconColor,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return InkWell(
      borderRadius: BorderRadius.circular(18.r),
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.all(20.r),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(18.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.05),
              blurRadius: 8.r,
              offset: Offset(0, 2.r),
            ),
          ],
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 60.r,
              height: 60.r,
              decoration: BoxDecoration(
                color: iconBg,
                borderRadius: BorderRadius.circular(16.r),
              ),
              child: Icon(icon, color: iconColor, size: 28.r),
            ),
            SizedBox(height: 16.r),
            Text(
              title,
              style: TextStyle(
                fontSize: 16.r,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 8.r),
            Text(
              subtitle,
              style: TextStyle(
                fontSize: 12.r,
                color: Color(0xFFBDBDBD),
              ),
              textAlign: TextAlign.center,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ],
        ),
      ),
    );
  }

  ///rate us
  // void _rateUs() async {
  //   RateMyApp rateMyApp = RateMyApp(
  //     preferencesPrefix: 'rateMyApp_',
  //     minDays: 0, // Show rate popup on first day of install.
  //     minLaunches: 0, // Show rate popup after 5 launches of app after minDays is passed.
  //     appStoreIdentifier: '6748627054',
  //     googlePlayIdentifier: 'com.sinosheets.app',
  //   );
  //   await rateMyApp.init();
  //   // if (mounted && rateMyApp.shouldOpenDialog) {
  //   rateMyApp.showRateDialog(
  //     context, title: 'Rate this app', // The dialog title.
  //     message: 'If you like this app, please take a little bit of your time to review it !\nIt really helps us and it shouldn\'t take you more than one minute.', // The dialog message.
  //     rateButton: 'RATE', // The dialog "rate" button text.
  //     noButton: 'NO THANKS', // The dialog "no" button text.
  //     laterButton: 'MAYBE LATER',
  //     ignoreNativeDialog: Platform.isAndroid, // Set to false if you want to show the Apple's native app rating dialog on iOS or Google's native app rating dialog (depends on the current Platform).
  //     dialogStyle: const DialogStyle(),
  //   );
  //   // }
  // }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    Get.delete<SettingsLogic>();
    super.dispose();
  }
}
